package com.project.myxima.views;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.project.myxima.R;
import com.project.myxima.adapters.PlayListAdapter;
import com.project.myxima.base.BaseApplication;
import com.project.myxima.utils.Constants;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.List;


public class PlayList_PopupWindow extends PopupWindow {

    private final View mPopView;
    private View mClose_tv;
    private RecyclerView mPlayList_rv;
    private PlayListAdapter mPlayListAdapter;
    private ImageView mPlayMode_iv;
    private TextView mPlayMode_tv;
    private View mPlayListContainer;
    private PlayListActionListener mPlayListClickListener = null;
    private ImageView mOrderIcon;
    private TextView mOrderText;
    private View mPlayOrder_container;

    public PlayList_PopupWindow() {

        //设置宽高
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        //将View加载进来
        mPopView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.pop_play_list, null);
        //设置内容
        setContentView(mPopView);
        setAnimationStyle(R.style.popupWinAnim);
        initView();
        initEvent();
    }


    private void initView() {
        mClose_tv = mPopView.findViewById(R.id.play_list_close_tv);
        mPlayList_rv = mPopView.findViewById(R.id.play_list_rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BaseApplication.getAppContext());

        mPlayList_rv.setLayoutManager(linearLayoutManager);
        mPlayListAdapter = new PlayListAdapter();
        mPlayList_rv.setAdapter(mPlayListAdapter);

        //PlayMode
        mPlayMode_iv = mPopView.findViewById(R.id.play_list_play_mode_switch_iv);
        mPlayMode_tv = mPopView.findViewById(R.id.play_list_play_mode_tv);

        mPlayListContainer = mPopView.findViewById(R.id.play_list_controller_container);

        //Play Order
        mPlayOrder_container = mPopView.findViewById(R.id.play_list_order_container);
        mOrderIcon = mPopView.findViewById(R.id.play_list_order_iv);
        mOrderText = mPopView.findViewById(R.id.play_list_order_tv);

    }

    private void initEvent() {
        mClose_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayList_PopupWindow.this.dismiss();
            }
        });

        mPlayListContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayListClickListener != null) {
                    mPlayListClickListener.onPlayModeClick();
                }
            }
        });

        mPlayOrder_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO: switch play order
                mPlayListClickListener.onPlayOrderClick();
            }
        });


    }

    /**
     * 给适配器设置数据
     * @param data
     */
    public void setListData(List<Track> data){
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setData(data);
        }
    }

    public void setCurrentPlayPosition(int playIndex) {
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setCurrentPlayPosition(playIndex);
        }

        mPlayList_rv.scrollToPosition(playIndex);
    }

    //item's click event

    /**
     *
     * @param listener
     */
    public void setonPlayListItemClickListener(onPlayListItemClickListener listener){
        mPlayListAdapter.setItemClickListener(listener);
    }

    public void updatePlayModeView(XmPlayListControl.PlayMode playMode) {
        int imageID = R.drawable.selector_play_mode_list_order;
        int textID = R.string.play_mode_list_order_text;
        switch (Constants.CURRENT_MODE){
            case PLAY_MODEL_LIST:
                imageID = R.drawable.selector_play_mode_list_order;
                textID = R.string.play_mode_list_order_text;
                break;
            case PLAY_MODEL_RANDOM:
                imageID = R.drawable.selector_play_mode_random;
                textID = R.string.play_mode_random_text;
                break;
            case PLAY_MODEL_LIST_LOOP:
                imageID = R.drawable.selector_play_mode_list_order_loop;
                textID = R.string.play_mode_list_order_loop_text;
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                imageID = R.drawable.selector_play_mode_single_loop;
                textID = R.string.play_mode_single_loop_text;
                break;
        }

        mPlayMode_iv.setImageResource(imageID);
        mPlayMode_tv.setText(textID);
    }

    public void updatePlayListOrderView(boolean isOrder){
        mOrderIcon.setImageResource(isOrder ? R.drawable.selector_play_mode_list_order : R.drawable.selector_play_list_revers_order);
        mOrderText.setText(isOrder? "顺序":"逆序");
    }

    public interface onPlayListItemClickListener{
        void onItemClick(int position);
    }

    /**
     * for playMode
     */

    public void setPlayListActionListener(PlayListActionListener listener){
        mPlayListClickListener = listener;
    }

    public interface PlayListActionListener {

        //Play Mode
        void onPlayModeClick();

        //PlayList Order
        void onPlayOrderClick();
    }

}
